<?php
/**
 * Created by PhpStorm.
 * User: robot
 * Date: 2018-04-20
 * Time: 11:46
 */

namespace app\admin\controller;


use app\base\BaseController;
use app\base\model\Message as MessageModel;
use think\Request;

class Message extends BaseController
{
    /***
     * 输出页面样式
     * @return mixed
     */
    public function index()
    {
        $this->assign('title', '留言板管理');
        return $this->fetch();
    }

    /***
     * 输出页面数据
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function ajax()
    {
        $request = Request::instance();
        if (!$request->isAjax()) {
            return ['code'=>-1,'msg'=>'请求方式错误'];
        }
        $message = new MessageModel();
        $limit = $request->param('limit');
        $offset = $limit * $request->param('page')-$limit;
        $total = $message->count('messageid');
        $data = $message->limit($offset,$limit)->select();
        return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $data];
    }

    /***
     * 留言板删除
     * @return array
     */
    public function dell()
    {
        $request = Request::instance();
        $messageid = $request->has('messageid')?$request->param('messageid'):false;
        if (!$messageid) {
            return ['code' => -1, 'msg' => '评论id未找到'];
        }

        if (MessageModel::destroy($messageid)) {
            return ['code' => 1, 'msg' => '删除成功'];
        } else {
            return ['code' => -1, 'msg' => '删除失败'];
        }
    }

}